//
//  MovieViewController.m
//  shipin
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableViewCell.h"
#import "TableViewHeaderView.h"
#import "MJRefresh.h"
#import "NetWorkHandle.h"
#import "MovieModel.h"
#import "MoviePlayViewController.h"
#import <RESideMenu.h>

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TableViewHeaderView  *headerView;
@property (nonatomic, strong) NSMutableArray *MovieDataArray;
@property (assign, nonatomic) int PagNum;
@property (nonatomic, strong) NSString *sender;

@end

@implementation MovieViewController

-(instancetype)init{
    if (self = [super init]) {
        self.title = @"视频";
        self.tabBarItem.image = [UIImage imageNamed:@"tabbar_movie"];
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{

    [self loadNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];


}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadtableView];
    [self loadData];
    [self loadMoreData];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FristCallBack:) name:@"First" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SecondCallBack:) name:@"Second" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ThridCallBack:) name:@"Thrid" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FourthCallBack:) name:@"Fourth" object:nil];
    
    
    
    
}
//懒加载初始化数组

- (NSMutableArray *)MovieDataArray
{
    if (!_MovieDataArray) {
        _MovieDataArray = [NSMutableArray array];
    }
    return _MovieDataArray;
}


- (void)FristCallBack:(NSNotification *)sender
{
    _sender = sender.object;
    [self loadData];
}
- (void)SecondCallBack:(NSNotification *)sender
{
    _sender = sender.object;
    [self loadData];
}
- (void)ThridCallBack:(NSNotification *)sender
{
    _sender = sender.object;
    [self loadData];
}
- (void)FourthCallBack:(NSNotification *)sender
{
    _sender = sender.object;
    [self loadData];
}


#pragma mark ---- 刷新 加载
- (void)loadData
{
    __weak __typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _PagNum = 0;
        if ([_sender isEqual:@"1"]) {
            [weakSelf loadFristNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else if ([_sender isEqual:@"2"]) {
            
            [weakSelf loadSecondNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else if ([_sender isEqual:@"3"]){
            
        [weakSelf loadThirdNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else if ([_sender isEqual:@"4"]){
            
        [weakSelf loadFourthNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
         }
    }];
    [self.tableView.header beginRefreshing];
}
- (void)loadMoreData
{
    
    __weak __typeof(self) weakSelf = self;
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _PagNum += 10;
        if ([_sender isEqual:@"1"]) {
            [weakSelf loadFristNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else if ([_sender isEqual:@"2"]) {
            
            [weakSelf loadSecondNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else if ([_sender isEqual:@"3"]){
            
            [weakSelf loadThirdNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else if ([_sender isEqual:@"4"]){
            
            [weakSelf loadFourthNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }else
        {
            [weakSelf loadNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        }
        
    }];
}








- (void)loadNetWorkData :(NSString *)str
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%@-10.html",str];
    if ([str isEqualToString:@"0"]) {
        [self cleanDataArray];
    }
    [NetWorkHandle getDataWithURLString:string compare:^(id object) {
        NSDictionary *dic = object;
        
        NSArray *arr = [dic objectForKey:@"videoList"];
        for (NSDictionary *d in arr) {
            MovieModel *model = [[MovieModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.MovieDataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

- (void)loadFristNetWorkData:(NSString *)str
{
    
    NSString *string = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/VAP4BFE3U/y/%@-10.html",str];
    if ([str isEqualToString:@"0"]) {
        [self cleanDataArray];
    }
    [NetWorkHandle getDataWithURLString:string compare:^(id object) {
        NSDictionary *dic = object;
        
        NSArray *arr = [dic objectForKey:@"VAP4BFE3U"];
        for (NSDictionary *d in arr) {
            MovieModel *model = [[MovieModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.MovieDataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

- (void)loadSecondNetWorkData:(NSString *)str
{
    
    NSString *string = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/VAP4BFR16/y/%@-10.html",str];
    if ([str isEqualToString:@"0"]) {
        [self cleanDataArray];
    }
    [NetWorkHandle getDataWithURLString:string compare:^(id object) {
        NSDictionary *dic = object;
        
        NSArray *arr = [dic objectForKey:@"VAP4BFR16"];
        for (NSDictionary *d in arr) {
            MovieModel *model = [[MovieModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.MovieDataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}


- (void)loadThirdNetWorkData:(NSString *)str
{
    
    NSString *string = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/VAP4BG6DL/y/%@-10.html",str];
    if ([str isEqualToString:@"0"]) {
        [self cleanDataArray];
    }
    [NetWorkHandle getDataWithURLString:string compare:^(id object) {
        NSDictionary *dic = object;
        
        NSArray *arr = [dic objectForKey:@"VAP4BG6DL"];
        for (NSDictionary *d in arr) {
            MovieModel *model = [[MovieModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.MovieDataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}



- (void)loadFourthNetWorkData:(NSString *)str
{
    
    NSString *string = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/VAP4BGTVD/y/%@-10.html",str];
    if ([str isEqualToString:@"0"]) {
        [self cleanDataArray];
    }
    [NetWorkHandle getDataWithURLString:string compare:^(id object) {
        NSDictionary *dic = object;
        
        NSArray *arr = [dic objectForKey:@"VAP4BGTVD"];
        for (NSDictionary *d in arr) {
            MovieModel *model = [[MovieModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.MovieDataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}
// 清除缓存
- (void)cleanDataArray
{
    _MovieDataArray = nil;
}




- (void)loadtableView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UINib *nib = [UINib nibWithNibName:@"MovieTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"cell"];
    _headerView  = [[[NSBundle mainBundle] loadNibNamed:@"TableViewHeaderView" owner:nil options:nil] firstObject];
    _headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
    
    self.tableView.tableHeaderView = _headerView;
    _headerView.tag = 1000;
    
    [self.view addSubview:self.tableView];
    
}

#pragma mark -- 实现tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _MovieDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MovieModel *model = _MovieDataArray[indexPath.row];
    cell.model = model;
    
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 400;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    MovieModel *model = _MovieDataArray[indexPath.row];
    MoviePlayViewController *playVC = [[MoviePlayViewController alloc] init];
    playVC.mp4_url = model.mp4_url;
    [self presentViewController:playVC animated:YES completion:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
